<?php

namespace app\apigovernment\controller;

use app\apigovernment\Validate\PersonageUserValidate;
use app\common\controller\Api;
use think\Config;
use think\Exception;

/**
 * 个人信息
 */
class PersonageUser extends Api
{
    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];

    protected $failException = true;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\apicompany\model\PersonageUser();
        $this->modelIntention = new \app\apicompany\model\Intention();
        $this->modelundergo = new \app\apicompany\model\Undergo();
        $this->modelEducation = new \app\apicompany\model\Education();
        $this->modelcultivate = new \app\apicompany\model\Cultivate();
        if (!Config::get('fastadmin.usercenter')) {
            $this->error(__('User center already closed'));
        }

    }

    /**
     * 列表
     * @return void
     */
    public function list()
    {
        $input = input();
        try {
            $pageArr = [
                'page' => 1,
                'page_limit' => 30,
            ];
            $where = [];
            if (!empty($input['id'])) {
                $where['id'] = $input['id'];
            }
            if (!empty($input['keyWork'])) {
                $where['name'] = ['like', '%' . $input['keyWork'] . '%'];
            }
            if (!empty($input['status'])) {
                $where['status'] = $input['status'];
            }
            $order = 'createtime asc';
            $pageArr = array_merge($pageArr, $input);
            $data = $this->model->alias('m')
                ->where($where)
                ->order($order)
                ->paginate([
                    'page' => $pageArr['page'] ?: 1,
                    'list_rows' => $pageArr['page_limit'] ?: 10
                ]);
//            foreach ($data as $k => &$v) {
//                $v['intention'] = $this->modelIntention->where('user_id', $v['user_id'])->select();
//                $v['undergo'] = $this->modelundergo->where('user_id', $v['user_id'])->select();
//                $v['education'] = $this->modelEducation->where('user_id', $v['user_id'])->select();
//                $v['cultivate'] = $this->modelcultivate->where('user_id', $v['user_id'])->select();
//            }
            $this->success('ok', $data);
        } catch (Exception $e) {
            $this->error($e->getMessage());
        }
    }

    /**
     * 审核
     * @return void
     */
    public function save(){
        $input = input('post.');
        try {
            $this->validate($input,PersonageUserValidate::class.'.save');
            $this->model->where('id',$input['id'])->update(['status' =>$input['status']]);
            $this->success('ok',['id' => $input['id']]);
        }catch (Exception $e){
            $this->error($e->getMessage());
        }
    }
}